package com.meta.act.app.system.repository.impl;

import com.meta.framework.util.BeanUtil;
import com.meta.act.app.domain.tenant.TenantDo;
import com.meta.act.app.domain.tenant.TenantExtDo;
import com.meta.act.app.domain.tenant.TenantFileDo;
import com.meta.act.app.system.mapper.TenantBaseMapper;
import com.meta.act.app.system.mapper.TenantExtMapper;
import com.meta.act.app.system.mapper.TenantFileMapper;
import com.meta.act.app.system.repository.TenantBaseRepository;
import com.meta.act.app.tenant.common.FileTypeEnum;
import com.meta.act.app.tenant.entity.TenantBase;
import com.meta.act.app.tenant.entity.TenantExt;
import com.meta.act.app.tenant.req.PageTenantQry;
import com.meta.act.app.tenant.req.ro.TenantBaseRo;
import com.meta.act.app.tenant.req.ro.TenantExtRo;
import com.meta.act.app.tenant.req.ro.TenantFileRo;
import com.meta.act.app.tenant.req.ro.TenantUserRo;
import com.meta.act.app.tenant.res.TenantPageIo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 *  租户信息表
 * @Title:      TenantBaseRepositoryImpl
 * @Author:     Simon
 * @Remark:     The modified content
 * @CreateDate: 2020-11-04 15:23:04
 * @Version:    v2.0
 */
@Service
public class TenantBaseRepositoryImpl implements TenantBaseRepository {

	@Autowired
	private TenantBaseMapper tenantBaseMapper;
	@Autowired
	private TenantExtMapper tenantExtMapper;
	@Autowired
	private TenantFileMapper tenantFileMapper;
	/**
	 * insert tenantBase info
	 * @method      insertTenantBase
	 * @param       tenantBase:
	 * @return      void
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public int insertTenantBase(TenantBase tenantBase){
			return tenantBaseMapper.insertTenantBase(tenantBase);
	}

	/**
	 * batch insert tenantBase info
	 * @method      insertTenantBase
	 * @param       tenantBases:
	 * @return      void
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public void batchInsertTenantBase(List<TenantBase> tenantBases){
			tenantBaseMapper.batchInsertTenantBase(tenantBases);
	}

	/**
	 * update tenantBase info
	 * @method      updateTenantBase
	 * @param       tenantBase:
	 * @return      int
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public int updateTenantBase(TenantBase tenantBase){
			return tenantBaseMapper.updateTenantBase(tenantBase);
	}

	/**
	 * update tenantBase info by Pk
	 * @method      updateTenantBaseByPk
	 * @param       tenantBase:
	 * @return      int
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public int updateTenantBaseByPk(TenantBase tenantBase){
		return tenantBaseMapper.updateTenantBaseByPk(tenantBase);
	}

	/**
	 * list tenantBase info
	 * @method      listTenantBase
	 * @param       tenantBase:
	 * @return      List<TenantBase>
	 * @author      Simon
	 * @date        2020-11-04 15:23:04
	 */
	@Override
	public List<TenantBase> listTenantBase(TenantBase tenantBase){
			return tenantBaseMapper.listTenantBase(BeanUtil.copyProperties(TenantBaseRo.class,tenantBase));
	}

	@Override
	public List<TenantBase> listTenantByUserId(TenantUserRo tenantUserRo) {
		return tenantBaseMapper.listTenantByUserId(tenantUserRo);
	}

	@Override
	public List<TenantPageIo> page(PageTenantQry qry) {
		return tenantBaseMapper.page(qry);
	}

	@Override
	public int pageCount(PageTenantQry qry) {
		return tenantBaseMapper.pageCount(qry);
	}

	@Override
	public TenantBase getTenantBase(Long tenantId) {
		if(tenantId == null ){
			return null;
		}
		TenantBaseRo ro = new TenantBaseRo();
		ro.setTenantId(tenantId);
		List<TenantBase> tenantBaseList = tenantBaseMapper.listTenantBase(ro);
		if(CollectionUtils.isEmpty(tenantBaseList))
		{
			return null;
		}
		return tenantBaseList.get(0);
	}
	@Override
	public TenantDo getTenantDo(Long tenantId) {
		if(tenantId == null ){
			return null;
		}
		TenantBaseRo ro = new TenantBaseRo();
		ro.setTenantId(tenantId);
		List<TenantBase> tenantBaseList = tenantBaseMapper.listTenantBase(ro);
		if(CollectionUtils.isEmpty(tenantBaseList))
		{
			return null;
		}
		TenantDo tenantDo = tenantBaseList.get(0).transform(TenantDo.class);
		// Long tenantId = tenantDo.getTenantId();
		TenantExtRo extRo = new TenantExtRo();
		extRo.setTenantId(tenantId);
		List<TenantExt> extList = tenantExtMapper.listTenantExt(extRo);
		if(!CollectionUtils.isEmpty(extList)){
			tenantDo.setTenantExtDo(extList.get(0).transform(TenantExtDo.class));
		}
		//文件
		TenantFileRo tenantFile = new TenantFileRo();
		tenantFile.setTenantId(tenantId);
		tenantFile.setFileType(FileTypeEnum.CREDIT_FILE.getCode());
		tenantDo.setCreditFileList(tenantFileMapper.listTenantFile(tenantFile).stream().map(x -> x.transform(TenantFileDo.class)).collect(Collectors.toList()));

		tenantFile = new TenantFileRo();
		tenantFile.setTenantId(tenantId);
		tenantFile.setFileType(FileTypeEnum.TAX_FILE.getCode());
		tenantDo.setTaxFileList(tenantFileMapper.listTenantFile(tenantFile).stream().map(x -> x.transform(TenantFileDo.class)).collect(Collectors.toList()));

		tenantDo.setTenantId(tenantId);
		return tenantDo;
	}
}
